﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TL_Cloud.Core.Extensions;
using TL_Cloud.Core.Interfaces;
using TL_Cloud.Models.BookShop;
using TL_Cloud.RepositoryInterface.BookShop;
using TL_Cloud.ViewModels.BookShop.BKBuyInfo;
using TL_Cloud.ViewModels.BookShop.BKCollect;
using TL_Cloud.ViewModels.BookShop.BKComment;
using TL_Cloud.ViewModels.BookShop.BKDownLoadRecord;
using TL_Cloud.ViewModels.BookShop.BKPics;
using TL_Cloud.ViewModels.BookShop.BKRecommend;

namespace TL_Cloud.API.Controllers
{
    /// <summary>
    /// 图书推荐信息
    /// </summary>
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class BKRecommendController : ControllerBase
    {
        private readonly ILogger logger;
        private readonly IUnitOfWork _unitOfWork;
        private readonly IBKRecommendRepository _repository;
        private readonly IUrlHelper _urlHelper;

        public BKRecommendController(
            ILogger<BKInfoController> logger,
            IBKRecommendRepository repository,
            IUnitOfWork unitOfWork
            )
        {

            this.logger = logger;
            this._repository = repository;
            this._unitOfWork = unitOfWork;
        }
        /// <summary>
        /// 获取所有的图书的推荐信息
        /// 搜索条件是:bookId
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        [HttpGet(Name = "GetBKRecommends")]
        public async Task<IActionResult> Get([FromQuery]BKReCommendQueryParameters parameters)
        {

            var dataList = await _repository.GetAllEntitiesAsync(parameters);

            //数据返回分页数据
            var meta = new
            {
                dataList.TotalItemsCount,
                dataList.PageSize,
                dataList.PageIndex,
                dataList.PageCount,
            };

            Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(meta, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            }));
            //返回结果塑性
            return Ok(dataList.ToDynamicIEnumerable(parameters.Fields));
        }

      
        /// <summary>
        /// 增加图书推荐信息
        /// </summary> 
        [HttpPost(Name = "CreateBkRecommend")]
        public async Task<IActionResult> Post([FromBody] Bk_Recommend entity)
        {
            if (entity == null)
            {
                return BadRequest();
            }
            
            var userinfo = this.GetUserInfo();
            entity.Create(userinfo.Item1, userinfo.Item2);

            _repository.Add(entity);

            if (!await _unitOfWork.SaveAsync())
            {
                throw new Exception("Save Failed!");
            } 
            return NoContent();
        }

       
        /// <summary>
        /// 删除图书推荐信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}", Name = "DeleteBKRecommend")]
        public async Task<IActionResult> Delete(string id)
        {
            var post = await _repository.GetEntityByIdAsync(id);
            if (post == null)
            {
                return NotFound();
            }
            _repository.Delete(post);

            if (!await _unitOfWork.SaveAsync())
            {
                throw new Exception($"Deleting entity {id} failed when saving.");
            }

            return NoContent();
        }
    }
}
